package Demo11;

/**
 * 最大连续1的个数
 * https://leetcode.cn/problems/max-consecutive-ones-iii/
 * 滑动窗口,来维护一个count 来解决问题 count 是0 的个数
 * 滑动窗口解题步骤
 * 1. 进窗口
 * 2. 判断
 *     出窗口
 * 更新返回值
 */

class Solution {
    // 缩略版
    public int longestOnes(int[] nums, int k) {
        int ret = 0;
        for(int left = 0,right = 0,count = 0;right < nums.length;right++) {
            if(nums[right] == 0) count++;// 进窗口
            while(count > k) {
                if(nums[left++] == 0)  count--;// 出窗口
            }
            ret = Math.max(ret,right - left + 1);// 判断
        }
        return ret;
    }
    public int longestOnes2(int[] nums, int k) {
        int ret = 0;
        for(int left = 0,right = 0,count = 0;right < nums.length;right++) {
            if(nums[right] == 0) {
                count++;
            }
            while(count > k) {
                if(nums[left] == 0) {
                    count--;
                }
                left++;
            }
            ret = Math.max(ret,right - left + 1);
        }
        return ret;
    }
    public int longestOnes1(int[] nums, int k) {
        int left = 0,right = 0,len = nums.length,ret = 0;
        int[] num = new int[2];
        while(right < len) {
            num[nums[right]]++;
            while(num[0] > k) {
                num[nums[left]]--;
                left++;
            }
            ret = Math.max(ret,right - left + 1);
            right++;
        }
        return ret;
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[] {1,1,1,0,0,0,1,1,1,1,0};
        int ret = solution.longestOnes(nums,2);
        System.out.println(ret);
    }
}